/**
 *
 */
package gemini.basic.dao.impl;

import java.util.List;

import gemini.basic.dao.GmnDao;
import gemini.basic.dao.UserDao;
import gemini.basic.model.User;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public User getByName(String name) {

		DetachedCriteria dtCri = DetachedCriteria.forClass(User.class);

		if (name != null && !("".equals(name))) {
			dtCri.add(Restrictions.eq("username", name));
		}

		List<Object> userList = gmnDao.findByCriteria(dtCri);

		User result = null;
		if (userList != null && !userList.isEmpty()) {
			result = (User) userList.get(0);
		}
		return result;
	}

	@Override
	public User saveOrUpdate(User user, boolean flush) {
		User result = null;
		if (flush) {
			result = (User) gmnDao.saveOrUpdateAndFlush(user);
		} else {
			result = (User) gmnDao.saveOrUpdate(user);
		}
		return result;
	}

}
